//车辆交易记录

interface ICarTradingRecordViewModel extends IBaseViewModel<ICarTradingRecordQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<TradingRecordListItemResponse>;
    /**
     * 总计承运次数
     */
    total:string;
    /**
     * 列表高
     */
    heightA:number;
}

interface ICarTradingRecordQueryTerms {
    /**
     * 货物名称
     */
    goodsName: string;
    /**
     * 货物数量
     */
    goodsNum: string;
    /**
     * 线路地址
     */
    route: string;
    /**
     * 发货地址
     */
    shipAddress: string;
    /**
     * 送货地址
     */
    deliverAddress: string;
}

class CarTradingRecordController extends ListController<ICarTradingRecordViewModel, ICarTradingRecordQueryTerms> {
    gridApi: uiGrid.IGridApiOf<TradingRecordListItemResponse>;
    driveruserId:string;

    constructor($scope: ICarTradingRecordViewModel,
     private carService: ICarService, 
     private valueService: IValueService,
        private $ngBootbox: BootboxService,
         private $location: ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();

        //this.carId = JSON.parse(window.localStorage.getItem("car").toString()).carId;
        this.driveruserId = JSON.parse(window.localStorage.getItem("car").toString()).carId;

        this.$scope.queryTerms = {
            goodsName: "",
            goodsNum: "",
            route: "",
            shipAddress: "",
            deliverAddress: ""
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "合同号", field: 'contractNumber', cellTemplate: '<div style="height:{{30 * row.entity.orderList.length}}px;line-height:{{30 * row.entity.orderList.length}}px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{row.entity.contractNumber}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: 88 },
                { displayName: "发货地址", field: 'originAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.originAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "送货地址", field: 'destinationAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.destinationAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '货物名称', field: 'goodsName', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.goodsName}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '货物数量', field: 'quantityOfGoods', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.quantityOfGoods}}{{item.goodsUnit}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, minWidth: 150 },
                { displayName: '外付单价', field: 'payablePrice', cellTemplate: '<div style="height:{{30 * row.entity.orderList.length}}px;line-height:{{30 * row.entity.orderList.length}}px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{row.entity.payablePrice}}{{row.entity.payablePriceUnit}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '发货日期', field: 'realDeliveryTime', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.realDeliveryTime}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                this.gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.query();
    }

    loadData(queryTerms: ICarTradingRecordQueryTerms, skip: number, count: number): void {
        this.carService.getTradingRecordList(this.driveruserId,"", queryTerms.goodsName, queryTerms.goodsNum,
            queryTerms.route, queryTerms.shipAddress, queryTerms.deliverAddress, skip, count).then(result => {
                // this.$scope.gridOptions.totalItems = result.data.total;
                // this.$scope.total = String(result.data.total);
                this.$scope.gridOptions.data = result.data.data;
                /**
                 * 设置列表高度
                 */
                let gridHArr = 0;
                result.data.data.forEach((item,index) => { gridHArr =  gridHArr + item.orderList.length; });
                this.$scope.heightA = (gridHArr*30)+96;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    getListCount():void{
        this.carService.getTradingRecordCount(this.driveruserId,"", this.$scope.queryTerms.goodsName,this.$scope.queryTerms.goodsNum,
        this.$scope.queryTerms.route, this.$scope.queryTerms.shipAddress, this.$scope.queryTerms.deliverAddress).then(res=>{
            this.$scope.total = String(res.data.extData);
            this.$scope.gridOptions.totalItems = res.data.extData;
        })
    }
}

angular.module("tms").controller("carTradingRecordController", ["$scope","carService", "valueService", "$ngBootbox", "$location", CarTradingRecordController]);